package cn.qdhlm.collectservice.core.component.analyse;


import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.common.util.ComUtil;

import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Name: AnyAfn00
 * @Description: 确认/否认（AFN=00H）
 * @Author: jiangcs
 */
@Log4j2
@Component
public class AnyAfn00 {

    @Autowired
    private UtilityService utilityService;

    public boolean execute(int da, int dt, int sn, String collectorNo, ByteBuf byteBuf) throws Exception {
        // F3：按数据单元标识确认和否认
        if (da == Constant.DA_0 && dt == Constant.DT_1024) {
            String confirmAfn = CodeUtil.byteToHexString(byteBuf.readByte());
            int confirmDa = byteBuf.readUnsignedShort();
            int confirmDt = byteBuf.readUnsignedShort();

            byteBuf.skipBytes(19);

            // 状态码
            String statusCode = CodeUtil.byteToHexString(byteBuf.readByte());

            // 获取命令信息
            String commandType = utilityService.getCmdType(confirmAfn, confirmDa, confirmDt);
            if (commandType != null) {
                log.info("execute: confirmAfn[{}], confirmDa[{}], confirmDt[{}], statusCode[{}]", confirmAfn, confirmDa, confirmDt, statusCode);

                // 更新命令结果状态
                Map<String, Object> resultMap = new HashMap<>(1);
                resultMap.put("statusCode", statusCode);
                boolean statusFlag = Constant.SYMBOL_00.equals(statusCode);
//                utilityService.updateCommandResultStatus(commandType, collectorNo, sn, resultMap, statusFlag ? 2 : 3);

                return statusFlag;
            }

            log.error("execute: confirmAfn[{}], confirmDa[{}], confirmDt[{}] commandType[{}] match fail", confirmAfn, confirmDa, confirmDt, commandType);
            return false;
        }

        log.error("execute: da[{}], dt[{}] match fail", da, dt);
        return false;
    }

    /**
     * 处理后续
     *
     * @param confirmAfn
     * @param confirmDa
     * @param confirmDt
     * @param collectorNo
     */
    private void processFollow(String confirmAfn, int confirmDa, int confirmDt, String collectorNo) {
        log.info("process follow start");

        switch (confirmAfn) {
            case "04":
                // F18：配置信息初始化
                if (confirmDa == Constant.DA_0 && confirmDt == Constant.DT_514) {

                    if (!ComUtil.isEmpty(collectorNo)) {
                        // 更新采集器状态：0-正常
//                        int count = collectorMapper.updateStatusById4RemoteConfig(collectorNo.getId(), 0);
//                        log.info("process follow: collectorNo[{}] update collector status[0] => {}", collectorNo, count);

                        // 更新终端状态：0-正常
//                        count = terminalMapper.updateStatusByCollectorId4RemoteConfig(collectorNo.getId(), 0);
//                        log.info("process follow: collectorNo[{}] update terminal status[0] => {}", collectorNo, count);
                    }
                }
                break;
            default:
                log.error("process follow: confirmData[{}] match fail", confirmDa);
        }

        log.info("process follow end");
    }
}
